<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName()
          + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>

<head>
  <title>停车记录</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <!-- 引入css样式文件 -->
  <!-- Bootstrap Core CSS -->
  <link href="<%=basePath%>css/bootstrap.css" rel="stylesheet"/>
  <!-- Bootstrap-grid CSS -->
  <link href="<%=basePath%>css/bootstrap-grid.css" rel="stylesheet"/>
  <!-- Bootstrap-reboot CSS -->
  <link href="<%=basePath%>css/bootstrap-reboot.css" rel="stylesheet"/>
  <!-- MetisMenu CSS -->

  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<style>

</style>
<body>

<div>
  <nav class="navbar navbar-expand-lg navbar-dark " style="background-color:#81D8CF;;">
    <img src="imges/logo.jpg" class="rounded float-left" alt="Logo" height="45" width="45">
    <div class="container" style="">
      <a class="navbar-brand" href="#">停车场管理系统</a>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText"
              aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="navbarText">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item">
            <a class="nav-link " href="${pageContext.request.contextPath }/user">用户信息</a>
          </li>
          <li class="nav-item">
            <a class="nav-link " href="${pageContext.request.contextPath }/book">预定信息</a>
          </li>
          <li class="nav-item">
            <a class="nav-link active" href="${pageContext.request.contextPath }/parking_record">车辆进出</a>
          </li>
          <li class="nav-item">
            <a class="nav-link " href="${pageContext.request.contextPath }/parking_space_manage">停车位管理</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath }/rate">收费标准</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath }/adminequip">设备管理</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="${pageContext.request.contextPath}/vehicle">车辆信息</a>
          </li>
        </ul>
        <span class="navbar-text">
          导航栏
        </span>
      </div>
    </div>
  </nav>
</div>

<%

  if (session.getAttribute("USER_SESSION")!=null) {
%>
<div class="container" >
  <div class="p-2 bg-light border">
    <div class="row">
      <div class="col"></div>
      <div class="col-6">
      <div class="panel panel-default">
        <div class="panel-body">
          <form class="form-horizontal" id="work">
            <div class="form-group">
              <label for="license_plate_number">识别到车牌号</label>
              <input type="text" class="form-control" id="license_plate_number"
                     value="${license_plate_number}" name="license_plate_number"/>
            </div>
            <button type="submit" class="btn btn-primary" onclick="createSpace()">操作</button>
          </form>
        </div>
      </div>
    </div>
      <div class="col"></div>
    </div>
  </div>
  <div class="p-2 bg-light border pt-md-5">
    <table class="table table-striped">
      <thead>
      <tr>
        <th>编号</th>
        <th>用户</th>
        <th>车牌</th>
        <th>停车位编号</th>
        <th>停车位类型</th>
        <th>进入时间</th>
        <th>离开时间</th>
        <th>费用</th>
        <td>操作</td>
      </tr>
      </thead>
      <tbody>
      <c:forEach items="${page.rows}" var="row">
        <tr>
          <td>${row.recordId}</td>
          <td>${row.user.username}</td>
          <td>${row.vehicle.licensePlateNumber}</td>
          <td>${row.space.spaceNumber}</td>
          <td>${row.space.spaceType}</td>
          <td>${row.entryTime}</td>
          <td>${row.exitTime}</td>
          <td>${row.cost}</td>
          <td>

            <a href="#" class="btn btn-danger btn-xs" onclick="deleteSpace(${row.recordId})">删除</a>
          </td>
        </tr>
      </c:forEach>
      </tbody>
    </table>
    <div class="col-md-12 text-right">
      <ujn:page url="${pageContext.request.contextPath }/parking_record"/>
    </div>
  </div>
</div>

<%
} else{
%>
<!--登陆失败界面-->
<div class="jumbotron jumbotron-fluid">
  <div class="container">
    <h1 class="display-4">欢迎您,登录后即可查看</h1>
    <p class="lead">
      <a href="${pageContext.request.contextPath}/login">Welcome, log in to view</a>
    </p>
  </div>
</div>
<%  }%>
<!-- 引入js文件 -->
<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.js"></script>
<!-- Bootstrap-bundle JavaScript -->
<script src="<%=basePath%>js/bootstrap.bundle.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>
</body>
<!-- 编写js代码 -->
<script>
  console.log("<%= basePath %>"); // 查看 basePath 是否正确
</script>
<script type="text/javascript">

  // 创建
  function createSpace() {
    $.post("<%=basePath%>record",
            $("#work").serialize(), function (data) {
              console.log("Data type: ", typeof data);
              console.log(data);
              if (data === "OK") {
                alert("停车记录创建成功！");
                window.location.reload();
              } else if(data ==="Busy"){
                alert("无空闲停车位！");
                window.location.reload();
              }
                else
               {
                alert("停车记录创建失败！");
                 window.location.reload();
              }
            });
  }

  // 删除
  function deleteSpace(id) {
    if (confirm('确实要删除该停车记录吗?')) {
      $.ajax({
        url: '<%=basePath%>record/' + id,
        type: 'DELETE',
        success: function (data) {
          if (data == "OK") {
            console.log(data);
            alert("停车记录信息删除成功！");
            window.location.reload();
          } else {
            alert("停车记录信息删除失败！");
            window.location.reload();
          }
        }
      });
    }
  }
</script>
</html>
